package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjSettlement;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjSettlementMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.NumberUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 工程报装-编制结算单条数据更新
 */
public class PjSettlemetUpdate implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
         try {
             PjSettlement pjSettlement = JsonUtils.objectToPojo(req.getData(),PjSettlement.class);

             PjSettlementMapper pjSettlementMapper = factory.getMapper(PjSettlementMapper.class);
             PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

             // 更新数据
             pjSettlement.setUpdateInfo(userInfo);
             pjSettlementMapper.update(pjSettlement);

             PjInfoBean pjInfoBean = pjInfoMapper.selectById(pjSettlement.getConsultId());
             // 计算合计金额 = 表前人工费结算 + 表后人工费结算 + 表前材料费结算 + 表后材料费结算 + 预算费结算 + 设计费结算 + 总管分摊费用
             BigDecimal totalMoney = new BigDecimal(0);
             // 表前人工费结算
             BigDecimal settlementLaborMeterBefore = pjSettlement.getSettlementLaborMeterBefore();
             // 表后人工费结算
             BigDecimal settlementLaborMeterAfter = pjSettlement.getSettlementLaborMeterAfter();
             // 表前材料费结算
             BigDecimal settlementMatrMeterBefore = pjSettlement.getSettlementMatrMeterBefore();
             // 表后材料费结算
             BigDecimal settlementMatrMeterAfter = pjSettlement.getSettlementMatrMeterAfter();
             // 预算费结算
             BigDecimal settlementCost = pjSettlement.getSettlementCost();
             // 设计费结算
             BigDecimal settlementDesignCost = pjSettlement.getSettlementDesignCost();
             // 总管分摊费用
             BigDecimal costShare = pjSettlement.getCostShare();
             totalMoney = totalMoney.add(settlementLaborMeterBefore);
             totalMoney = totalMoney.add(settlementLaborMeterAfter);
             totalMoney = totalMoney.add(settlementMatrMeterBefore);
             totalMoney = totalMoney.add(settlementMatrMeterAfter);
             totalMoney = totalMoney.add(settlementCost);
             totalMoney = totalMoney.add(settlementDesignCost);
             totalMoney = totalMoney.add(costShare);
             // 红线内费用
             totalMoney = totalMoney.add(new BigDecimal(pjSettlement.getRedLineInMoney() == null ? 0 : pjSettlement.getRedLineInMoney()));
             // 红线外费用
             totalMoney = totalMoney.add(new BigDecimal(pjSettlement.getRedLineOutMoney() == null ? 0 : pjSettlement.getRedLineOutMoney()));
             // 器具费用
             totalMoney = totalMoney.add(new BigDecimal(pjSettlement.getUtensilMoney() == null ? 0 : pjSettlement.getUtensilMoney()));

             // 计算预缴金额（工程收款之和）
             PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
             PjCollectionQuery pjCollectionQuery = new PjCollectionQuery();
             pjCollectionQuery.setProjectCode(pjInfoBean.getBillNo());
             BigDecimal collectionMoney = PjCollectionCountMoney.getTotalMoney(pjCollectionMapper,pjInfoMapper,pjCollectionQuery);

             // 表前材料费结算
             pjInfoBean.setSetmtMatrMeterBefore(settlementMatrMeterBefore.doubleValue());
             // 表前人工费结算
             pjInfoBean.setSetmtLaborMeterBefore(settlementLaborMeterBefore.doubleValue());
             // 表后材料费结算
             pjInfoBean.setSetmtMatrMeterAfter(settlementMatrMeterAfter.doubleValue());
             // 表后人工费结算
             pjInfoBean.setSetmtLaborMeterAfter(settlementLaborMeterAfter.doubleValue());
             // 预算费结算
             pjInfoBean.setSetmtBudgetCost(settlementCost.doubleValue());
             // 设计费结算
             pjInfoBean.setSetmtDesignCost(settlementDesignCost.doubleValue());
             // 红线内费用
             pjInfoBean.setRedLineInMoney(pjSettlement.getRedLineInMoney());
             // 红线外费用
             pjInfoBean.setRedLineOutMoney(pjSettlement.getRedLineOutMoney());
             // 器具费用
             pjInfoBean.setUtensilMoney(pjSettlement.getUtensilMoney());
             // 工程造价
             pjInfoBean.setCostShare(costShare.doubleValue());
             // 结算日期
             pjInfoBean.setSetmtDate(new Date());
             // 结算合计金额
             pjInfoBean.setSetmtMoney(totalMoney.doubleValue());
             // 余额退款方式
             pjInfoBean.setRefundWay(pjSettlement.getRefundType());
             // 退款银行
             pjInfoBean.setBankName(pjSettlement.getRefundBank());
             // 退款银行账户
             pjInfoBean.setBankAccount(pjSettlement.getRefundAccount());
             // 退款银行账户名
             pjInfoBean.setBankAccountName(pjSettlement.getRefundName());
             // 退补金额
             pjInfoBean.setBackFillMoney(NumberUtils.subtract(totalMoney.doubleValue(), collectionMoney.doubleValue()));
             pjInfoBean.setUpdateInfo(userInfo);
             // 同步保存至工程主表的结算字段
             pjInfoMapper.savePreSetmt(pjInfoBean);

             // 2. 返回结果
             return MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"编制结算单条数据更新成功",null);
         } catch (Exception e) {
             logger.error("编制结算单条数据更新失败",e);
             return MessageBean.create(Constant.MESSAGE_INT_FAIL,"编制结算单条数据更新失败",null);
         }
    }
}
